package com.zw.system.controller;


import com.zw.object.result.Result;
import com.zw.object.system.SysMenu;
import com.zw.object.vo.AssginMenuVo;
import com.zw.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author zw
 * @since 2022-12-07
 */
@Api(tags = "菜单管理接口")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    /**
     *          获取所有菜单接口
     * @return  Result
     */
    @ApiOperation("获取菜单")
    @GetMapping("/findNodes")
    public Result findNodes(){
        List<SysMenu> menuList = sysMenuService.findNodes();
        return Result.ok(menuList);
    }

    /**
     *                      新增菜单接口
     * @param permission    参数实体类对象
     * @return              Result
     */
    @PreAuthorize("hasAnyAuthority('bnt.sysMenu.add')")
    @ApiOperation("新增菜单")
    @PostMapping("/save")
    public Result save(@RequestBody SysMenu permission){
        return sysMenuService.saveMenu(permission);
    }

    @PreAuthorize("hasAnyAuthority('bnt.sysMenu.update')")
    @ApiOperation("修改菜单")
    @PostMapping("/update")
    public Result updateMenu(@RequestBody SysMenu permission){
        return sysMenuService.updateMenu(permission);
    }

    /**
     *              根据ID删除菜单接口
     * @param id    ID
     * @return      Result
     */
    @PreAuthorize("hasAnyAuthority('bnt.sysMenu.remove')")
    @ApiOperation("根据菜单ID删除")
    @DeleteMapping("/delete/{id}")
    public Result deleteMenuById(@PathVariable String id){
        return sysMenuService.deleteMenuById(id);
    }


    /**
     *                  根据角色获取菜单接口
     * @param roleId    角色ID参数
     * @return          Result
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    @ApiOperation("根绝角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result findMenuByRoleId(@PathVariable("roleId")String roleId){
        return sysMenuService.findMenuByRoleId(roleId);
    }

    /**
     *                          保存角色菜单
     * @param assignMenuVo      参数实体对象
     * @return                  Result
     */
    @ApiOperation("给角色分配菜单")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assignMenuVo){
        return sysMenuService.saveMenuByRoleId(assignMenuVo);
    }


}

